package com.kaifamiao.config;

import com.kaifamiao.interceptor.TokenInterceptor;
import com.kaifamiao.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Slf4j
@Configuration
public class InterceptorConfiguration implements WebMvcConfigurer {

    @Autowired
    private TokenService tokenService;

    // <bean id="tokenInterceptor" class="com.kaifamiao.interceptor.TokenInterceptor" scope="singleton" />
    @Bean
    @Scope("singleton")
    public TokenInterceptor getTokenInterceptor(){
        log.info("创建拦截器实例");
        TokenInterceptor ti = new TokenInterceptor();
        ti.setTokenService(tokenService);
        return ti;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("注册拦截器");
        // 获得 拦截器实例
        TokenInterceptor ti = getTokenInterceptor();
        // 注册拦截器
        InterceptorRegistration registration = registry.addInterceptor( ti );
        registration.addPathPatterns("/user/**", "/file/**");
        registration.excludePathPatterns("/user/login/*", "/user/register/*");
    }

}
